import com.mysql.jdbc.Connection;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:DELL
 * Date:2025-03-27
 * Time:21:01
 */
public class Demo1 {
    public static void main(String[] args) throws SQLException {
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入学号");
        int id=scanner.nextInt();
        System.out.println("请输入姓名");
        String name=scanner.next();
        //1.先创建Datasourse
        DataSource dataSource=new MysqlDataSource();
        ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/wang?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("123456");
        //2.建立和数据库服务器之间的连接，连接好了之后，才能进行后序的请求-响应工作。
        Connection connection= (Connection) dataSource.getConnection();
        //3.构造sql
        //查询
        String sql="insert into student values(?,?)";
        //删除
        //String sql="delete from student where id=1";
        //修改
        //String sql="update student set name ='张三' where id=100";
        PreparedStatement statement=connection.prepareStatement(sql);
        statement.setInt(1,id);
        statement.setString(2,name);
        //4.把sql发给服务器。
        statement.executeUpdate();
        //4.释放资源，关闭连接
        statement.close();
        connection.close();
    }
}
